package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.project.entity.OutOrder;
import com.project.entity.PreOutOrder;
import com.project.entity.Product;
import com.project.mapper.OutOrderInfoMapper;
import com.project.mapper.OutOrderMapper;
import com.project.mapper.PreOutOrderMapper;
import com.project.mapper.ProductMapper;
import com.project.service.PreOutOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lovo
 * @since 2023-07-21
 */
@Service
public class PreOutOrderServiceImp extends ServiceImpl<PreOutOrderMapper, PreOutOrder> implements PreOutOrderService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private OutOrderMapper outOrderMapper;
    @Autowired
    private OutOrderInfoMapper outOrderInfoMapper;

    @Override
    public List<PreOutOrder> findById(Integer customId, Integer userId) {
        return this.baseMapper.findById(customId,userId);
    }

    @Override
    public boolean UpdateOutNum(Integer preOutId, Integer productId, Integer newOutNum) {
        // 根据商品ID查询商品表库存数量是否 >= 新出库数量
        QueryWrapper<Product> qw1 = new QueryWrapper<>();
        qw1.eq("pk_productId",productId);
        Product product = productMapper.selectOne(qw1);
        //  判断库存是否充足
        if (newOutNum <= product.getStoreNum()){//  库存充足
            //  按出库ID、商品ID查询出出库对象
            QueryWrapper<PreOutOrder> qw2 = new QueryWrapper<>();
            qw2.eq("pk_preOutOrderId",preOutId);
            qw2.eq("fk_productId",productId);
            PreOutOrder preOutOrder = this.baseMapper.selectOne(qw2);
            // 设置新数量
            preOutOrder.setOutNum(newOutNum);
            //  修改出库数量
            this.baseMapper.updateById(preOutOrder);
            return true;
        }
        return false;
    }

    @Override
    public void addProductById(Integer productId, Integer customId, Integer sellerId) {
        //  按商品ID、客户ID、销售员ID查询出库表中记录数
        int num = this.baseMapper.countPreOrderNum(productId,customId,sellerId);
        if (num == 0){
            PreOutOrder preOutOrder = new PreOutOrder();
            preOutOrder.setProductId(productId);
            preOutOrder.setCustomId(customId);
            preOutOrder.setUserId(sellerId);
            preOutOrder.setOutNum(1);// 初始出库数量默认为1
            //  添加新出库记录
            this.baseMapper.insert(preOutOrder);
        }else {
            //  按商品ID、客户ID、销售员ID查询出出库对象
            QueryWrapper<PreOutOrder> qw = new QueryWrapper<>();
            qw.eq("fk_productId",productId);
            qw.eq("fk_customId",customId);
            qw.eq("fk_userId",sellerId);
            PreOutOrder preOutOrder = this.baseMapper.selectOne(qw);
            System.out.println(preOutOrder);
            // 设置新出库数量（出库数量+1）
            preOutOrder.setOutNum(preOutOrder.getOutNum() + 1);
            //  添加新出库记录
            this.baseMapper.updateById(preOutOrder);
        }
    }

    @Override
    public void delProductById(Integer productId, Integer customId, Integer sellerId) {
        QueryWrapper<PreOutOrder> qw = new QueryWrapper<>();
        qw.eq("fk_productId",productId);
        qw.eq("fk_customId",customId);
        qw.eq("fk_userId",sellerId);
        this.baseMapper.delete(qw);
    }

    @Override
    public void outBound(OutOrder outOrder) {
        //  按客户ID查询出库单集合
        List<PreOutOrder> preList = this.baseMapper.findListByCustomId(outOrder.getCustomId());
        //  计算出库商品总价，封装为订单总价;修改商品库存。
        int totalPrice = 0;
        for (PreOutOrder preOutOrder:preList) {
            //  计算商品总价
            totalPrice += preOutOrder.getOutNum() * preOutOrder.getProduct().getSellPrice();
            //  计算出出库后的商品库存
            int productStoreNum = preOutOrder.getProduct().getStoreNum() - preOutOrder.getOutNum();
            //  修改商品表库存记录
            productMapper.updateStoreNum(preOutOrder.getProductId(),productStoreNum);
        }
        //  设置商品总价
        outOrder.setTotalPrice(totalPrice);
        //  添加新销售单（同时返回销售单ID）
        outOrder.setDate(LocalDate.now().toString());// 获取当前时间并填充订单时间属性
        outOrderMapper.insert(outOrder);
        //  添加销售详情表记录
        outOrderInfoMapper.addOutOrderInfo(outOrder.getOutOrderId(),preList);
        //根据客户ID，销售员ID删除该客户入库表中记录
        QueryWrapper<PreOutOrder> qw = new QueryWrapper<>();
        qw.eq("fk_customId",outOrder.getCustomId());
        qw.eq("fk_userId",outOrder.getUserId());
        this.baseMapper.delete(qw);
    }
}
